import { FakeApi, random } from '@wowon/utils';

// const initData = Array.from({ length: random(8, 25) }).map((_, i) => {
//   return {
//     key: `${i}`,
//     title: '',
//     desc: '',
//     error: ''
//   };
// });

export const errorList = new FakeApi([], {
  queryType: {
    title: 'like',
  },
});

export default {
  'GET /mock/error/query': async (req, res) => {
    const result = await errorList.query(req.query);
    res.json(result);
  },
  'POST /mock/error/report': async (req, res) => {
    try {
      if (random(6) > 1) {
        const result = await errorList.create(req.body);
        res.json({ ...result, message: '上报成功！' });
        return;
      }
      throw Error('上报失败！');
    } catch (error) {
      await errorList.create({ title: '后端错误', desc: '上报失败！', error: '/mock/error/report' });
      res.json({ code: '-1', message: '上报失败！' });
    }
  },
};
